import EventEmitter from "events";
import http = require("http")

/*===================================================
客户端进入游戏先登录账号, 获得UIN以及MD5KEY, 后续通讯需要MD5验证权限  MD5()



===================================================*/

export class ZPacket{
    public strGroup:string  = "";
    public nCmd:number = -1;
    public vParams:string[] = [];
    public pClient:http.ServerResponse  = null;

    public static Create(vStr:string[]){
        if(vStr.length <= 2)
            return null;
        var pP = new ZPacket();
        pP.strGroup = vStr[0];  //第一位模块识别码
        pP.nCmd = parseInt(vStr[1]);    //第二位功能代码
                                        //长度及MD5校验

        pP.vParams  = vStr.slice(2,vStr.length);
        return pP;
    }

    public GetPosVal(pos:number):string{
        if(pos >= this.vParams.length)
            return "";
        return this.vParams[pos];
    }
}

export default class EventCenter extends EventEmitter{
    public static pECenter: EventCenter = null;
    public static getInstance(): EventCenter {
        if (EventCenter.pECenter == null) {
            EventCenter.pECenter = new EventCenter();
        }
        return EventCenter.pECenter;
    }

    //注册时间监听
    public RegPacketListen(strE:string,fCB:Function){
        this.removeListener(strE,()=>{});
        this.on("/"+strE,function(params) {
            fCB(params);
        });
    }

    public DispatchPack(pP:ZPacket){

        this.emit(pP.strGroup,pP);
    }
}